`
https://leetcode.cn/problems/defuse-the-bomb/
`

/**
 * @param {number[]} code
 * @param {number} k
 * @return {number[]}
 */
var decrypt = function (code, k) {
  const n = code.length
  const res = new Array(n).fill(0)
  if (k === 0) return res

  // 维护一个长度为 k 的滑动窗口，把窗口内的数字和放到对应的位置上即可
  let sum = 0
  let left = 0, right = 0
  while (right < n + Math.abs(k) - 1) {
    const n1 = code[right < n ? right : right % n]
    sum += n1
    right++

    if (right - left >= Math.abs(k)) {
      // 更新答案
      const targetIdx = k > 0 ? left - 1 : right
      res[targetIdx >= 0 ? targetIdx % n : targetIdx % n + n] = sum

      // 缩小窗口
      const n2 = code[left < n ? left : left % n]
      sum -= n2
      left++
    }
  }

  return res
};